package com.bite.book.service;

import com.bite.book.Enum.BookStatus;
import com.bite.book.dao.BookDao;
import com.bite.book.mapper.BookInfoMapper;
import com.bite.book.model.BookInfo;
import com.bite.book.model.PageRequest;
import com.bite.book.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class BookService {
    @Autowired
    private BookInfoMapper bookInfoMapper;
    private BookDao bookDao = new BookDao();
    public List<BookInfo> getList(){
        List<BookInfo> books = bookDao.mockData();
        for(BookInfo book:books){
            if(book.getStatus() == 1){
                book.setStatusCN("可借阅");
            }else {
                book.setStatusCN("不可借阅");
            }
        }
        return books;
    }

    public Integer addBook(BookInfo bookInfo) {
        return bookInfoMapper.addBook(bookInfo);
    }

    public PageResult<BookInfo> getListByPage(PageRequest pageRequest) {
        Integer count = bookInfoMapper.count();

        List<BookInfo> books =  bookInfoMapper.getListByPage(pageRequest.getOffset(), pageRequest.getPageSize());
        //把状态码和状态对应起来
        for(BookInfo book: books){
            book.setStatusCN(BookStatus.getNameByCode(book.getStatus()));
        }
        return new PageResult<>(count, books, pageRequest);
    }

    public BookInfo queryBookById(Integer id) {
        return bookInfoMapper.queryBookBuId(id);
    }

    public void updateBook(BookInfo bookInfo) {
        bookInfoMapper.updateBook(bookInfo);
    }

    public void deleteBook(Integer id) {
        BookInfo bookInfo = new BookInfo();
        bookInfo.setId(id);
        bookInfo.setStatus(0);
        bookInfoMapper.updateBook(bookInfo);
    }

    public void batchDeleteBook(List<Integer> ids) {
        bookInfoMapper.batchDeleteBook(ids);
    }
}
